package org.yelongframework.baidu.aip.ocr.executor;

import java.util.HashMap;
import java.util.Objects;

import org.yelongframework.baidu.aip.ocr.result.OcrAipResult;
import org.yelongframework.baidu.aip.ocr.result.OcrAipResultFactory;

import com.baidu.aip.ocr.AipOcr;

/**
 * 默认实现
 * 
 * @since 1.0.0
 */
public class DefaultOcrAipExecutor implements OcrAipExecutor {

	/** OCR接口调用客户端 */
	private final AipOcr aipOcr;

	/** OCR结果工厂 */
	private final OcrAipResultFactory ocrAipResultFactory;

	// ==================================================构造器==================================================

	/**
	 * @param aipOcr              OCR接口调用客户端
	 * @param ocrAipResultFactory OCR结果工厂
	 */
	public DefaultOcrAipExecutor(AipOcr aipOcr, OcrAipResultFactory ocrAipResultFactory) {
		this.aipOcr = Objects.requireNonNull(aipOcr, "aipOcr");
		this.ocrAipResultFactory = Objects.requireNonNull(ocrAipResultFactory, "ocrAipResultFactory");
	}

	// ==================================================执行基础通用OCR识别==================================================

	@Override
	public OcrAipResult executeBasicGeneral(String image, HashMap<String, String> options) {
		options = null == options ? new HashMap<>() : options;
		return ocrAipResultFactory.create(aipOcr.basicGeneral(image, options));
	}

	@Override
	public OcrAipResult executeBasicGeneral(byte[] image, HashMap<String, String> options) {
		options = null == options ? new HashMap<>() : options;
		return ocrAipResultFactory.create(aipOcr.basicGeneral(image, options));
	}

	// ==================================================执行通用OCR识别(含位置)==================================================

	@Override
	public OcrAipResult executeGeneral(String image, HashMap<String, String> options) {
		options = null == options ? new HashMap<>() : options;
		return ocrAipResultFactory.create(aipOcr.general(image, options));
	}

	@Override
	public OcrAipResult executeGeneral(byte[] image, HashMap<String, String> options) {
		options = null == options ? new HashMap<>() : options;
		return ocrAipResultFactory.create(aipOcr.general(image, options));
	}

	// ==================================================执行高精度通用OCR识别(含位置)==================================================

	@Override
	public OcrAipResult executeAccurateGeneral(String image, HashMap<String, String> options) {
		options = null == options ? new HashMap<>() : options;
		return ocrAipResultFactory.create(aipOcr.accurateGeneral(image, options));
	}

	@Override
	public OcrAipResult executeAccurateGeneral(byte[] image, HashMap<String, String> options) {
		options = null == options ? new HashMap<>() : options;
		return ocrAipResultFactory.create(aipOcr.accurateGeneral(image, options));
	}

	// ==================================================OCR接口调用客户端==================================================

	@Override
	public AipOcr getAipClient() {
		return aipOcr;
	}

	// ==================================================OCR结果工厂==================================================

	@Override
	public OcrAipResultFactory getAipResultFactory() {
		return ocrAipResultFactory;
	}

}
